using Quartz;
using Vin.Extension.Framework.Quartz.ServiceInterface;
using Vin.Tool.AspNetCore.Singleton;

namespace Vin.Extension.Framework.Quartz.Listener;

public class VinJobListener : IJobListener
{
    public string Name => "VinJobListener";

    public async Task JobToBeExecuted(IJobExecutionContext context,
        CancellationToken cancellationToken = new CancellationToken())
    {
        // 准备执行任务
        await VinApp.GetRequiredServiceByHttpContext<IExecuteJobService>().ExecuteAsync(context);
        // Console.WriteLine($"Job {context.JobDetail.Key} is going to be executed.");
    }

    public async Task JobExecutionVetoed(IJobExecutionContext context,
        CancellationToken cancellationToken = new CancellationToken())
    {
        // 任务被否决（中断）
        await VinApp.GetRequiredServiceByHttpContext<IExecuteJobService>().VetoedAsync(context);
        // Console.WriteLine($"Job {context.JobDetail.Key} execution was vetoed.");
    }

    public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException? jobException,
        CancellationToken cancellationToken = new CancellationToken())
    {
        // 任务执行完成
        await VinApp.GetRequiredServiceByHttpContext<IExecuteJobService>().CompleteAsync(context, jobException);
        // var jobId = context.JobDetail.Key.Name.ToLong();
        // Console.WriteLine($"Job {context.JobDetail.Key} was executed.");
        // var jobService = VinApp.GetRequiredServiceByHttpContext<ISysJobService>();
        // await jobService.CompleteAsync(jobId, jobException);
    }
}